/**
 * @author ming
 * @date 2021/8/7
 */
package org.lq;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

//代码生成器
public class KuangCode {
    // 需要构建一个代码自动生成器
    public static void main(String[] args) {
        AutoGenerator autoGenerator = new AutoGenerator();
        //配置策略
        //1.全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");//获取用户的目录
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("ming");
        gc.setOpen(false);
        gc.setFileOverride(false);//是否覆盖
        gc.setServiceName("%sService");//去service 的I前缀
        gc.setIdType(IdType.AUTO);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);
        autoGenerator.setGlobalConfig(gc);
        //设置数据源
        DataSourceConfig DSC = new DataSourceConfig();
        DSC.setUrl("jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        DSC.setDbType(DbType.MYSQL);
        DSC.setDriverName("com.mysql.cj.jdbc.Driver");
        DSC.setUsername("root");
        DSC.setPassword("123456");
        autoGenerator.setDataSource(DSC);
        //3.包的配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("blog");
        pc.setParent("org.lq.autoGeneratorMybatisPlus");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        autoGenerator.setPackageInfo(pc);
        //4.策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("user");//设置要映射的表
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);//下划线转驼峰命名
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);//下划线转驼峰命名
        strategyConfig.setEntityLombokModel(true);//自动生成lombok
        strategyConfig.setLogicDeleteFieldName("deleted");//自动·生成逻辑删除配置
        //设置自动填充配置
        TableFill create_date = new TableFill("create_date", FieldFill.INSERT);
        TableFill update_date = new TableFill("update_date ", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(create_date);
        tableFills.add(update_date);
        strategyConfig.setTableFillList(tableFills);
        //乐观锁
        strategyConfig.setVersionFieldName("version");
        strategyConfig.setRestControllerStyle(true);//localhost:8080/hello_id_2
        autoGenerator.setStrategy(strategyConfig);
        //执行
        autoGenerator.execute();


    }
}
